clear all
global g_root "C:\Users\Hongye Guo\Dropbox (Personal)\Density\Code\Production\Submission"
local l_date : display %tdCYND date(c(current_date), "DMY")
local l_time = substr(c(current_time), 1, 2) + substr(c(current_time), 4, 2)

use "$g_root\Data\daily_vwretd_202106.dta", clear
drop if mi(vwretd)
gen ym = mofd(date)
sort ym date
forvalues i = 1/5{
	by ym: gen ym_vwretd_d`i' = vwretd[`i']
}
keep ym ym_v*
duplicates drop
tempfile tf_first
save `tf_first', replace

use "$g_root\Data\vwretd_clean_202106.dta", clear
gen month = month(dofm(ym+1))
gen am_flag = mod(month, 3) == 1

sort ym
forvalues i = 1/12{
	gen vwretd_l`i'm = vwretd[_n-`i']
}
gen vwretd_f1m = vwretd[_n+1]
gen vwretd_l0m = vwretd
forvalues i = 1/4{
	local j = (`i'-1)*3 
	local jp1 = `j' + 1
	local jp2 = `j' + 2
	foreach l_var in vwretd{
		gen `l_var'_prevq1_l`i'q = `l_var'_l`j'm if mod(month,3)==2
		replace `l_var'_prevq1_l`i'q = `l_var'_l`jp1'm if mod(month,3)==0
		replace `l_var'_prevq1_l`i'q = `l_var'_l`jp2'm if mod(month,3)==1
	}
}	
gen vwretd_prevq1_t4q = vwretd_prevq1_l1q + vwretd_prevq1_l2q + vwretd_prevq1_l3q + vwretd_prevq1_l4q

replace ym = ym+1
merge 1:1 ym using `tf_first', keep(1 3) nogen
replace ym = ym-1

gen vwretd_f5d = (1+ym_vwretd_d1)*(1+ym_vwretd_d2)*(1+ym_vwretd_d3)*(1+ym_vwretd_d4)*(1+ym_vwretd_d5)-1
gen vwretd_f1m_nfw = (1 + vwretd_f1m)/(1+ym_vwretd_d1)/(1+ym_vwretd_d2)/(1+ym_vwretd_d3)/(1+ym_vwretd_d4)/(1+ym_vwretd_d5) - 1    

tsset ym
gen vwretd_nfw = L1.vwretd_f1m_nfw
forvalues i = 1/12{
	gen vwretd_nfw_l`i'm = L`i'.vwretd_nfw
}
gen vwretd_nfw_l0m = vwretd_nfw
forvalues i = 1/4{
	local j = (`i'-1)*3 
	local jp1 = `j' + 1
	local jp2 = `j' + 2
	foreach l_var in vwretd_nfw{
		gen `l_var'_prevq1_l`i'q = `l_var'_l`j'm if mod(month,3)==2
		replace `l_var'_prevq1_l`i'q = `l_var'_l`jp1'm if mod(month,3)==0
		replace `l_var'_prevq1_l`i'q = `l_var'_l`jp2'm if mod(month,3)==1
	}
}
gen vwretd_nfw_prevq1_t4q = vwretd_nfw_prevq1_l1q + vwretd_nfw_prevq1_l2q + vwretd_nfw_prevq1_l3q + vwretd_nfw_prevq1_l4q

clear matrix
local col_count = 0

reg vwretd_f1m vwretd_prevq1_t4q if mod(month, 3) == 1, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
reg vwretd_f5d vwretd_prevq1_t4q if mod(month, 3) == 1 & !mi(vwretd_f1m), robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	
reg vwretd_f1m_nfw vwretd_prevq1_t4q if mod(month, 3) == 1, robust
local col_count = `col_count' + 1
estimates store sep_`col_count'	

esttab sep_* using "$g_root/Output/FirstWkOut_`l_date'`l_time'.csv", star(* 0.1  ** 0.05 *** 0.01) b(3) t(2) r2 br noomit replace 
